/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taskmanager2ndedition.server;

import taskmanager2ndedition.User;
import taskmanager2ndedition.exceptions.SessionNotFoundException;
import java.util.*;

/**
 *
 * @author denaas
 */
public class SessionManager {    
    private static int currentSessionId = 0;
    private List<Session> sessions = new LinkedList<Session>();
    
    public Session startSession(User user){
        int id = currentSessionId;
        Session session = new Session(id, user);
        sessions.add(session);
        currentSessionId++;
        return session;
    }
    
    public void closeSession(int sessionId) throws SessionNotFoundException{
        Session theSession = findSession(sessionId);
        if (theSession != null){
            sessions.remove(theSession);
        }        
    }
    
    public Session findSession(int sessionId) throws SessionNotFoundException{
        Session theSession = null;
        for(Session it: sessions){
            if (it.getId() == sessionId){
                theSession = it;
            }
        }
        if (theSession == null)
            throw new SessionNotFoundException();
        return theSession;
    }
    
    
}
